﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Moon.Sql
{
    public class MySqlDialect : SqlDialectProvide
    {
        public override string SetSqlName(string name)
        {
            return $"`{name}`";
        }

        public override string GetDate { get { return "now()"; } }

        public override string ToDateFormat(object field, string style)
        {
            switch (style)
            {
                case "yyyy-MM-dd HH:mm:ss":
                    return $"date_format({field},'%Y-%m-%d %H:%i:%s')";
                case "yyyy-MM-dd HH:mm":
                    return $"date_format({field},'%Y-%m-%d %H:%i')";
                case "yyyy-MM-dd HH":
                    return $"date_format({field},'%Y-%m-%d %H')";
                case "yyyy-MM-dd":
                    return $"date_format({field},'%Y-%m-%d')";
                case "yyyy-MM":
                    return $"date_format({field},'%Y-%m')";
                case "yyyy/MM/dd/ HH:mm:ss":
                    return $"date_format({field},'%Y/%m/%d %H%i%s')";
                case "yyyy/MM/dd HH:mm":
                    return $"date_format({field},'%Y/%m/%d %H%i')";
                case "yyyy/MM/dd HH":
                    return $"date_format({field},'%Y/%m/%d %H')";
                case "yyyy/MM/dd":
                    return $"date_format({field},'%Y/%m/%d')";
                case "yyyy/MM":
                    return $"date_format({field},'%Y/%m')";
                case "yyyy":
                    return $"date_format({field},'%Y')";
                case "HH:mm:ss":
                    return $"date_format({field},'%H:%i:%s')";
            }
            throw new ArgumentException($"date format error({style})");
        }

        public override string Datediff(string dateType, string column, string dt)
        {
            return $"timestampdiff({dateType},{column},{dt})";
        }

        public override string IsNull(object column, object val)
        {
            if (val == null || val.ToString() == "") val = "''";
            return $"ifnull({column},{val})";
        }

        public override string Guid { get { return "md5(uuid())"; } }

        public override string Length(string field) { return $"char_length({field})"; }

        public override string PartitionBy(object partitionByField, object orderByField, Moon.Sql.OrderBy orderBy = OrderBy.Asc)
        {
            throw new Exception("function not supported mysql");
        }

        public override string IndexOf(string field, object val)
        {
            return $"locate({field}, {val}) - 1";
        }

        public override string Year(string field)
        {
            return $"year({field})";
        }

        public override string Month(string field)
        {
            return $"month({field})";
        }

        public override string Day(string field)
        {
            return $"dayofmonth({field})";
        }

        public override string Hour(string field)
        {
            return $"hour({field})";
        }

        public override string ToString(object field)
        {
            return $"cast({field} as char)";
        }

        public override string ToInt(object field)
        {
            return $"cast({field} as signed)";
        }
    }
}
